package com.highstockings.Interceptor;

import com.highstockings.service.ArticleService;
import com.highstockings.service.GalleryTypeService;
import com.highstockings.service.ModelService;
import com.highstockings.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 管理后台拦截器
 * Created by diablo13064 on 2018/3/29.
 */
public class AdminInterceptor implements HandlerInterceptor {
    BeanFactory factory = null;
    @Autowired
    ArticleService articleService;
    @Autowired
    ModelService modelService;
    @Autowired
    GalleryTypeService galleryTypeService;
    @Autowired
    UserService userService;
    private Logger logger = LoggerFactory.getLogger(ContentInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        //System.out.println("preHandle......" + httpServletRequest.getRequestURI());
        /*HttpSession session = httpServletRequest.getSession();
        if (session.getAttribute("currentUser") == null) {
            httpServletRequest.getRequestDispatcher("/admin/login").forward(httpServletRequest, httpServletResponse);
        }*/
        factory = WebApplicationContextUtils.getRequiredWebApplicationContext(httpServletRequest.getServletContext());
        if (articleService == null) {
            articleService = (ArticleService) factory.getBean("ArticleService");
        }
        if (modelService == null) {
            modelService = (ModelService) factory.getBean("ModelService");
        }
        if (galleryTypeService == null) {
            galleryTypeService = (GalleryTypeService) factory.getBean("GalleryTypeService");
        }
        if (userService == null) {
            userService = (UserService)factory.getBean("UserService");
        }
        /*User user = userService.selectUserById(1);
        if (user != null && userService.selectUserByNamePass(user.getUsername(), user.getPassword()) != null) {
            //System.out.println(user.getUsername());
            return true;
        }*/
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
        //1.左侧边栏模特信息（模特图片，模特名alias,模特评分。模特其他）

        //2.套图信息（套图封面 套图名 ，其他）
        //4、套图分类信息（套图名，查询）
        modelAndView.addObject("modelList", modelService.findAllModel());
        modelAndView.addObject("galleryTypeList", galleryTypeService.findAllGalleryType());
    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

        //日志处理
        //UserAgent userAgent = UserAgent.parseUserAgentString(httpServletRequest.getHeader("User-Agent"));
        //userAgent.
        /*synchronized (){

        }*/
    }
}
